package com.lesama.test;

import com.lesama.domain.Movie;

import java.util.Scanner;

public class MovieService {
    private Movie[] movies; // 存储外界传入的movies数组
    private Scanner scanner = new Scanner(System.in); // 将Scanner对象作为类的成员，可以在类中共享
    public MovieService(Movie[] movies) {
        this.movies = movies;
    }

    public void start() {

        lo:
        while (true) {
            System.out.println("请输入您的选择");
            System.out.println("1. 查询全部电影信息");
            System.out.println("2. 根据id查询电影");
            System.out.println("3. 退出");
            int option = scanner.nextInt();
            switch (option) {
                case 1:
                    // System.out.println("1. 查询全部电影信息");
                    queryAllMovies();
                    break;
                case 2:
                    // System.out.println("2. 根据id查询电影信息");
                    queryMovieById();
                    break;
                case 3:
                    System.out.println("3. 退出");
                    break lo;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }

    private void queryMovieById() {
        // 监听用户键盘录入的电影id
        System.out.println("请输入要查询的电影id");
        int id = scanner.nextInt();
        // 根据id遍历movies数组查询
        for (int i = 0; i < movies.length; i++) {
            if(movies[i].getId() == id) {
                System.out.println(movies[i].getId() + "---" + movies[i].getTitle()
                + "---" + movies[i].getTime() + "---" + movies[i].getScore() + "---"
                + movies[i].getArea() + "---" + movies[i].getType() + "---" + movies[i].getDirector()
                + "---" + movies[i].getStarring());
                return;
            }
        }
        // 此处说明用户输入的电影id查询不到
        System.out.println("很抱歉，您输入的电影id不存在");
    }

    private void queryAllMovies() {
        for (int i = 0; i < movies.length; i++) {
            System.out.println(movies[i].getTitle() + "---" + movies[i].getScore());
        }
    }
}
